<?php
namespace app\push;
use think\worker\Server;
use Channel\Client;

//发布
class Publish extends Server{
	protected $socket = 'http://0.0.0.0:2222';
	protected $option = ['name'=>'publish'];

	public function onWorkerStart($worker){
		 //Channel客户端连接到Channel服务端
		Client::connect('127.0.0.1',1111);
	}
	
	public function onMessage($connection,$data){
		$connection->send("OOOK");
		if(!isset($_GET['content'])){
			return;
		}
		if(isset($_GET['worker_id']) && isset($_GET['connection_id'])){
			//@one http://worker.juzi38hao.cn:2222/?content=@&connection_id=3&worker_id=0
			$data = [
				'connection_id'=>$_GET['connection_id'],
				'content'=>$_GET['content']
			];
			Client::publish($_GET['worker_id'],$data);
		}else{
			//@all http://worker.juzi38hao.cn:2222/?content=all
			$data['content'] = $_GET['content'];
			Client::publish('broadcast',$data);
		}
		
	}
}

